/*****************************************************************************************
 *                                                                                       *
 * OpenSpace                                                                             *
 *                                                                                       *
 * Copyright (c) 2014-2020                                                               *
 *                                                                                       *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this  *
 * software and associated documentation files (the "Software"), to deal in the Software *
 * without restriction, including without limitation the rights to use, copy, modify,    *
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to    *
 * permit persons to whom the Software is furnished to do so, subject to the following   *
 * conditions:                                                                           *
 *                                                                                       *
 * The above copyright notice and this permission notice shall be included in all copies *
 * or substantial portions of the Software.                                              *
 *                                                                                       *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   *
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A         *
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    *
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  *
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE  *
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                         *
 ****************************************************************************************/

#version 460  core

; // preprocessor add semicolon to isolate error messages
#line 1 1 // C:\os\clean\OpenSpace-0.16.0\shaders\PowerScaling\powerScaling_vs.hglsl
/*****************************************************************************************
 *                                                                                       *
 * OpenSpace                                                                             *
 *                                                                                       *
 * Copyright (c) 2014-2020                                                               *
 *                                                                                       *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this  *
 * software and associated documentation files (the "Software"), to deal in the Software *
 * without restriction, including without limitation the rights to use, copy, modify,    *
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to    *
 * permit persons to whom the Software is furnished to do so, subject to the following   *
 * conditions:                                                                           *
 *                                                                                       *
 * The above copyright notice and this permission notice shall be included in all copies *
 * or substantial portions of the Software.                                              *
 *                                                                                       *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   *
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A         *
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    *
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  *
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE  *
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                         *
 ****************************************************************************************/

#ifndef POWERSCALING_VS_H_HGLSL
#define POWERSCALING_VS_H_HGLSL

uniform vec4 campos;
uniform mat4 camrot;
uniform vec2 scaling;
uniform vec4 objpos;

; // preprocessor add semicolon to isolate error messages
#line 1 2 // C:\os\clean\OpenSpace-0.16.0\shaders\PowerScaling\powerScalingMath.hglsl
/*****************************************************************************************
 *                                                                                       *
 * OpenSpace                                                                             *
 *                                                                                       *
 * Copyright (c) 2014-2020                                                               *
 *                                                                                       *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this  *
 * software and associated documentation files (the "Software"), to deal in the Software *
 * without restriction, including without limitation the rights to use, copy, modify,    *
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to    *
 * permit persons to whom the Software is furnished to do so, subject to the following   *
 * conditions:                                                                           *
 *                                                                                       *
 * The above copyright notice and this permission notice shall be included in all copies *
 * or substantial portions of the Software.                                              *
 *                                                                                       *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   *
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A         *
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    *
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  *
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE  *
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                         *
 ****************************************************************************************/

#ifndef POWERSCALING_MATH_HGLSL
#define POWERSCALING_MATH_HGLSL


const float k = 10.0;
const float FLT_MAX = 1e38; // Not max but large enough for the purpose

float log10(float x) {
    return log(x) / log(10.0);
}

vec4 psc_normalization(vec4 v) {
    vec4 v2 = v;
    float l = length(v.xyz);
    v2.xyz = v.xyz / l;
    v2.w = v.w - log10(1.0/l);
    return v2;
}

vec4 psc_addition(vec4 v1, vec4 v2) {
    float ds = v2.w - v1.w;
    if(ds >= 0) {
        float p = pow(k,-ds);
        return vec4(v1.x*p + v2.x, v1.y*p + v2.y, v1.z*p + v2.z, v2.w);
    } else {
        float p = pow(k,ds);
        return vec4(v1.x + v2.x*p, v1.y + v2.y*p, v1.z + v2.z*p, v1.w);
    }
}

vec4 psc_subtraction(vec4 v1, vec4 v2) {
    vec4 negV2 = vec4(-v2.xyz, v2.w);
    return psc_addition(v1, negV2);
}

vec2 psc_addition(vec2 v1, vec2 v2) {
    float ds = v2.y - v1.y;
    if(ds >= 0) {
        float p = pow(k,-ds);
        return vec2(v1.x*p + v2.x, v2.y);
    } else {
        float p = pow(k,ds);
        return vec2(v1.x + v2.x*p, v1.y);
    }
}

vec2 psc_subtraction(vec2 v1, vec2 v2) {
    vec2 negV2 = vec2(-v2.x, v2.y);
    return psc_addition(v1, negV2);
}


vec4 z_normalization(vec4 v_in) {
    vec4 v_out = v_in;
    v_out.z = 0;
    //if ( v_out.z < -v_out.w )
    //  v_out.z = -v_out.w;
    //else if (v_out.z > v_out.w)
    //  v_out.z = v_out.w;
    return v_out;
}

/**
 * Convert a psc vector to a float
 */
vec3 pscToLinear(vec4 position) {
    return pow(k, position.w) * position.xyz;
}

/**
 * Convert a psc scalar to a float
 */
float pscToLinear(vec2 position) {
    return pow(k, position.y) * position.x;
}

#endif
; // preprocessor add semicolon to isolate error messages
#line 34 1 // C:\os\clean\OpenSpace-0.16.0\shaders\PowerScaling\powerScaling_vs.hglsl

vec4 psc_to_meter(vec4 v1, vec2 v2) {
    float factor = v2.x * pow(k,v2.y + v1.w);
    return vec4(v1.xyz * factor, 1.0);
}

vec4 psc_scaling(vec4 v1, vec2 v2) {
    float ds = v2.y - v1.w;
    if(ds >= 0) {
        return vec4(v1.xyz * v2.x * pow(k,v1.w), v2.y);
    } else {
        return vec4(v1.xyz * v2.x * pow(k,v2.y), v1.w);
    }
}

// vertexPosition is returned as the transformed vertex in OS Camera Rig Space in PSC
vec4 pscTransform(inout vec4 vertexPosition, mat4 modelTransform) {
    vec3 local_vertex_pos = mat3(modelTransform) * vertexPosition.xyz;
    
    // PSC addition; local vertex position and the object power scaled world position
    vertexPosition = psc_addition(vec4(local_vertex_pos,vertexPosition.w),objpos);
    
    // PSC addition; rotated and viewscaled vertex and the cameras negative position
    vertexPosition = psc_addition(vertexPosition,vec4(-campos.xyz,campos.w));
    
    // rotate the camera
    vertexPosition.xyz =  mat3(camrot) * vertexPosition.xyz;
    vec4 tmp = vertexPosition;
    vertexPosition = psc_scaling(vertexPosition, scaling);
    
    // project using the rescaled coordinates,
    tmp = psc_to_meter(tmp, scaling);
    
    // Return the vertex tranformed to OS Camera Rig Space
    // in meters.
    return tmp;
}

#endif
; // preprocessor add semicolon to isolate error messages
#line 28 0 // C:\os\clean\OpenSpace-0.16.0\modules\base\shaders\plane_vs.glsl

layout(location = 0) in vec4 in_position;
layout(location = 1) in vec2 in_st;

out vec2 vs_st;
out float vs_screenSpaceDepth;
out vec4 vs_positionScreenSpace;
// G-Buffer
out vec4 vs_gPosition;
out vec3 vs_gNormal;

uniform mat4 modelViewProjectionTransform;
uniform mat4 modelViewTransform;

void main() {
    vec4 position = vec4(in_position.xyz * pow(10, in_position.w), 1);
    vec4 positionClipSpace = modelViewProjectionTransform * position;
    vec4 positionScreenSpace = z_normalization(positionClipSpace);

    gl_Position = positionScreenSpace;

    // G-Buffer
    vs_gNormal = vec3(0.0);
    vs_gPosition = vec4(modelViewTransform * position); // Must be in SGCT eye space;

    vs_st = in_st;
    vs_screenSpaceDepth = positionScreenSpace.w;
}
